﻿using System.IO;
using System.Text;

namespace SVTTranslateTextEngine
{
    /// <summary>
    /// File access auxiliary object
    /// </summary>
    public class FileAccessHelper
    {
        public static string ReadTextContext(string szFilePath)
        {
            string szResult = null;

            if (!string.IsNullOrEmpty(szFilePath) && File.Exists(szFilePath))
            {
                StreamReader srReader = null;
                try
                {
                    srReader = new StreamReader(szFilePath, Encoding.UTF8);
                    szResult = srReader.ReadToEnd();
                }
                finally
                {
                    if (srReader != null)
                    {
                        srReader.Dispose();
                        srReader.Close();
                    }
                }
            }
            return szResult;
        }

        public static bool WriteTextContext(string szContext, string szFilePath)
        {
            bool bResult = false;
            if (!string.IsNullOrEmpty(szFilePath) && szFilePath.LastIndexOf("\\") > -1)
            {
                string szDirPath = szFilePath.Substring(0, szFilePath.LastIndexOf("\\"));
                if (Directory.Exists(szDirPath))
                {
                    StreamWriter sWriter = null;
                    try
                    {
                        sWriter = new StreamWriter(szFilePath);
                        sWriter.Write(szContext);
                        bResult = true;
                    }
                    finally
                    {
                        if (sWriter != null)
                        {
                            sWriter.Dispose();
                            sWriter.Close();
                        }
                    }
                }
            }
            return bResult;
        }
    }
}